<?php
namespace app\beautiful\controller\admin\mall;
use app\BaseController;
use app\beautiful\model\mall\FreightTemplateModel;
use app\beautiful\model\mall\FreightTemplatePieceModel;

class FreightTemplate extends BaseController{
    protected $name='运费模板';

    public function show()
    {
        if(null === $freight_template = FreightTemplateModel::where('shop_id',$this->request->shopId)->find()){
            $freight_template = new FreightTemplateModel();
            $freight_template->shop_id = $this->request->shopId;
            $freight_template->no_delivery_area = [];
            $freight_template->save();
        }

        if($freight_template->freight_type==2){
            if($freight_template->valuation_type==1){
                if(null === $freight_template_piece = FreightTemplatePieceModel::where('freight_template_id',$freight_template->freight_template_id)->find()){
                    $freight_template_piece = new FreightTemplatePieceModel();
                    $freight_template_piece->shop_id = $this->request->shopId;
                    $freight_template_piece->freight_template_id = $freight_template->freight_template_id;
                    $freight_template_piece->piece_data = [];
                    $freight_template_piece->save();
                }
            }
        }

        $piece=[
            'base_piece_num'=>isset($freight_template_piece) ? $freight_template_piece->base_piece_num : '',
            'base_piece_price'=>isset($freight_template_piece) ? $freight_template_piece->base_piece_price : '',
            'add_piece_num'=>isset($freight_template_piece) ? $freight_template_piece->add_piece_num : '',
            'add_piece_price'=>isset($freight_template_piece) ? $freight_template_piece->add_piece_price : '',
            'piece_data'=>isset($freight_template_piece) ? $freight_template_piece->piece_data : [],
        ];

        $detail=[
            'name'=>$freight_template->name,
            'hours'=>$freight_template->hours,
            'freight_type'=>$freight_template->freight_type,
            'valuation_type'=>$freight_template->valuation_type,
            'no_delivery_area'=>$freight_template->no_delivery_area,
        ];

        $return=[
            'detail'=>$detail,
            'piece'=>$piece
        ];

        return $this->success($return);
    }

    public function save()
    {
        $name=$this->request->param('name');
        $hours=$this->request->param('hours/d',0);
        $freight_type=$this->request->param('freight_type/d',1);
        $valuation_type=$this->request->param('valuation_type/d',1);
        $no_delivery_area=$this->request->param('no_delivery_area/a',[]);

        $areas=[];
        if(empty($name)) return $this->error('请输入名称');
        if(empty($hours) || $hours<=0) return $this->error('请选择发货时间');
        if(empty($freight_type) || !in_array($freight_type,[1,2])) return $this->error('请选择邮费设置');
        if(empty($valuation_type) || !in_array($valuation_type,[1])) $valuation_type=1;
        if(!empty($no_delivery_area)){
            foreach ($no_delivery_area as $v){
                if(in_array($v,$areas)) return $this->error('暂不发货地区的'.$v.'已经被其他选中，您无法选择');
                if(!in_array($v,$areas)) $areas[]=$v;
            }
        }

        //按件
        if($freight_type==2 && $valuation_type==1){
            $base_piece_num=$this->request->param('base_piece_num/d',0);
            $base_piece_price=$this->request->param('base_piece_price/f',0);
            $add_piece_num=$this->request->param('add_piece_num/d',0);
            $add_piece_price=$this->request->param('add_piece_price/f',0);
            $piece_data=$this->request->param('piece_data','','security_json_or_array');

            if(empty($base_piece_num) || $base_piece_num<=0) return $this->error('请输入基础件数');
            if(empty($base_piece_price) || $base_piece_price<=0) return $this->error('请输入基础价格');
            if(empty($add_piece_num) || $add_piece_num<=0) return $this->error('请输入附加件数');
            if(empty($add_piece_price) || $add_piece_price<=0) return $this->error('请输入附加价格');
            if(!empty($piece_data)){
                foreach ($piece_data as $k => $v){
                    if(empty($v['designated_area']) || !is_array($v['designated_area'])) return $this->error('请选择第'.($k+1).'行的指定区域');
                    foreach ($v['designated_area'] as $val){
                        if(in_array($val,$areas)) return $this->error('第'.($k+1).'行发货地区的'.$val.'已经被其他选中，您无法选择');
                        if(!in_array($val,$areas)) $areas[]=$val;
                    }
                    if(empty($v['base_piece_num']) || $v['base_piece_num']<=0) return $this->error('请输入第'.($k+1).'行的基础件数，且不能少于0');
                    if(empty($v['base_piece_price']) || $v['base_piece_price']<=0) return $this->error('请输入第'.($k+1).'行的基础价格，且不能少于0');
                    if(empty($v['add_piece_num']) || $v['add_piece_num']<=0) return $this->error('请输入第'.($k+1).'行的附加件数，且不能少于0');
                    if(empty($v['add_piece_price']) || $v['add_piece_price']<=0) return $this->error('请输入第'.($k+1).'行的附加价格，且不能少于0');
                }
            }
        }

        //操作模板
        if(null === $freight_template = FreightTemplateModel::where('shop_id',$this->request->shopId)->find()){
            $freight_template = new FreightTemplateModel();
            $freight_template->shop_id = $this->request->shopId;
        }
        $freight_template->name = $name;
        $freight_template->hours = $hours;
        $freight_template->freight_type = $freight_type;
        $freight_template->valuation_type = $valuation_type;
        $freight_template->no_delivery_area = $no_delivery_area;
        $freight_template->save();

        //操作计件
        if($freight_type==2 && $valuation_type==1){
            if(null === $freight_template_piece = FreightTemplatePieceModel::where('freight_template_id',$freight_template->freight_template_id)->find()){
                $freight_template_piece = new FreightTemplatePieceModel();
                $freight_template_piece->shop_id = $this->request->shopId;
                $freight_template_piece->freight_template_id = $freight_template->freight_template_id;
            }

            $freight_template_piece->base_piece_num = $base_piece_num;
            $freight_template_piece->base_piece_price = $base_piece_price;
            $freight_template_piece->add_piece_num = $add_piece_num;
            $freight_template_piece->add_piece_price = $add_piece_price;
            $freight_template_piece->piece_data = $piece_data;
            $freight_template_piece->save();
        }

        return $this->success('设置成功');

    }
}
